package com.org.beauty.salon.utils;

import lombok.Data;

import java.io.Serializable;

/**
 * 对外包装数据工具
 *
 * @author : zhangyanbin
 * @date : 2020/8/26 14:00
 */
@Data
public class WebResult<S> implements Serializable {

    public static final Integer SUCCESS = 200;
    public static final Integer ERROR = 500;

    private Status status;
    private S data;
    private Long timestamp = System.currentTimeMillis();

    /**
     * 内部通用函数
     **/

    private static <S> WebResult<S> response(Status status, S data) {
        WebResult<S> webResp = new WebResult<>();
        webResp.setStatus(status);
        webResp.setData(data);
        return webResp;
    }

    /**
     * 对外暴露函数
     **/

    public static <S> WebResult<S> success(S data, Integer code, String message) {
        return response(Status.generate(code, message), data);
    }

    public static WebResult successMessage(String message) {
        return success(null, SUCCESS, message);
    }

    public static <S> WebResult<S> success() {
        return success(null, SUCCESS, null);
    }

    public static <S> WebResult<S> successData(S data) {
        return success(data, SUCCESS, null);
    }

    public static <S> WebResult<S> success(S data, String message) {
        return success(data, SUCCESS, message);
    }

    public static <S> WebResult<S> error(S data, Integer code, String message) {
        return response(Status.generate(code, message), data);
    }

    public static <S> WebResult<S> error(String message) {
        return error(null, ERROR, message);
    }

    public static WebResult<String> error(Integer code, String message) {
        return error(null, code, message);
    }


}
